﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mIE.UI
{
    public partial class Frm_IE_Reference_Add : frmBaseChild
    {
        DataTable dt_Routing = new DataTable();
        DataTable dt_SizeParm = new DataTable();
        DataTable dt_SizeFoam = new DataTable();
        DataTable dt_Feature = new DataTable();
        DataTable dt_Cycle = new DataTable();
        public Frm_IE_Reference_Add()
        {
            InitializeComponent();
        }

        private void Frm_IE_Reference_Add_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        public override void DoSave(IButtonInfo sender)
        {
            StringBuilder sbPartNo = new StringBuilder();
            DataTable dtTemp1 = Common.GetTable.GetTable_Routing();
            DataTable dtTemp2 = Common.GetTable.GetSizeParmList_Fabric();
            DataTable dtTemp3 = Common.GetTable.GetTable_Feature();
            DataTable dtTemp4 = Common.GetTable.GetSizeParmList_Foam();
            DataTable dtTemp5 = Common.GetTable.GetCycleTime();
            string update_TaskList = "";
            try
            {
                if (dt_Routing.Rows.Count > 0 && dt_Feature.Rows.Count > 0 && dt_Cycle.Rows.Count>0)
                {
                    string[] str1 = memoEdit1.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    if (str1.Length > 0)
                    {
                        sbPartNo.AppendFormat(" AND PART_NO IN('{0}') ", string.Join("','", str1.ToArray()));
                    }
                    //判断是否存在任务清单表
                    string sql_taskList = @"select distinct PART_NO,PROD_CATEGORY,ROUTING_FLAG,PARM_FLAG,FEAT_FLAG FROM [LamexMESManager].[dbo].[IE_Part_TaskList] where 1=1 " + sbPartNo + "";
                    DataTable dt1 = BaseBuildSql.GetDataTableBySql(sql_taskList);
                    if (dt1.Rows.Count != str1.Length)
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("批量维护号码不存在任务清单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    //判断是否同一类型
                    string sql_type = @" select PROD_CATEGORY FROM [LamexMESManager].[dbo].[IE_Part_TaskList] where PART_NO='" + txtReference_No.Text.Trim() + "'";
                    DataTable dt2 = BaseBuildSql.GetDataTableBySql(sql_type);
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        if (dt1.Rows[i]["PROD_CATEGORY"].ToString() != dt2.Rows[0]["PROD_CATEGORY"].ToString())
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("批量维护号码与参考号码类型不一致！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        //判断尺寸标识、工艺标识、特征标识是否有值
                        if (dt1.Rows[i]["ROUTING_FLAG"].ToString().Trim().Length > 0 || dt1.Rows[i]["PARM_FLAG"].ToString().Trim().Length > 0 ||
                            dt1.Rows[i]["FEAT_FLAG"].ToString().Trim().Length > 0)
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("批量维护号码【路线标识】或【尺寸标识】或【特征标识】已维护！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    //更新任务清单表
                    if (dt_SizeFoam.Rows.Count > 0 || dt_SizeParm.Rows.Count > 0)
                    {

                        update_TaskList = @" update [LamexMESManager].[dbo].[IE_Part_TaskList] set ROUTING_FLAG='1',PARM_FLAG='1',FEAT_FLAG='1'
                                         where 1=1 " + sbPartNo + "";
                    }
                    else
                    {
                        update_TaskList = @" update [LamexMESManager].[dbo].[IE_Part_TaskList] set ROUTING_FLAG='1',FEAT_FLAG='1'
                                         where 1=1 " + sbPartNo + "";
                    }
                    BaseBuildSql.ExecuteNonQuery(update_TaskList);
                    //更新工艺路线表
                    foreach (var routing in str1)
                    {
                        DataRow dr = dtTemp1.NewRow();
                        dr["DWG_NO"] = "NIL";
                        dr["PART_NO"] = routing;
                        dr["GROUP_CODE"] = dt_Routing.Rows[0]["GROUP_CODE"].ToString();
                        dr["LAST_SAVE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                        dtTemp1.Rows.Add(dr);
                    }
                    //更新尺寸参数表【布】
                    foreach (var fabric in str1)
                    {
                        //DataRow dr = dtTemp2.NewRow();
                        //dr["DWG_NO"]="NIL";
                        //dr["PART_NO"]=size;
                        //dr["WIDTH_1"] = dt_SizeParm.Rows[0]["WIDTH_1"].ToString();
                        //dr["WIDTH_2"] = dt_SizeParm.Rows[0]["WIDTH_2"].ToString();
                        //dr["DEPTH_1"] = dt_SizeParm.Rows[0]["DEPTH_1"].ToString();
                        //dr["DEPTH_2"] = dt_SizeParm.Rows[0]["DEPTH_2"].ToString();
                        //dr["THICK"] = dt_SizeParm.Rows[0]["THICK"].ToString();
                        //dr["LENGTH"] = dt_SizeParm.Rows[0]["LENGTH"].ToString();
                        //dr["WIDTH"] = dt_SizeParm.Rows[0]["WIDTH"].ToString();
                        //dr["HEIGHT"] = dt_SizeParm.Rows[0]["HEIGHT"].ToString();
                        //dr["DEPTH"] = dt_SizeParm.Rows[0]["DEPTH"].ToString();
                        //dr["LATEST_SAVE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        //dr["UPDATE_USER"]= Lamex.Common.Loginer.CurrentUser.Account;
                        //dtTemp2.Rows.Add(dr);
                        for (int i = 0; i < dt_SizeParm.Rows.Count; i++)
                        {
                            DataRow dr = dtTemp2.NewRow();
                            dr["PART_NO"] = fabric;
                            dr["MOUDLE_NAME"] = dt_SizeParm.Rows[i]["MOUDLE_NAME"].ToString();
                            dr["FABRIC_TYPE"] = dt_SizeParm.Rows[i]["FABRIC_TYPE"].ToString();
                            dr["REQUIRE_QTY"] = dt_SizeParm.Rows[i]["REQUIRE_QTY"].ToString();
                            dr["FRONT_SIZE"] = dt_SizeParm.Rows[i]["FRONT_SIZE"].ToString();
                            dr["FRONT_QTY"] = dt_SizeParm.Rows[i]["FRONT_QTY"].ToString();
                            dr["BACK_SIZE"] = dt_SizeParm.Rows[i]["BACK_SIZE"].ToString();
                            dr["BACK_QTY"] = dt_SizeParm.Rows[i]["BACK_QTY"].ToString();
                            dr["LEFT_SIZE"] = dt_SizeParm.Rows[i]["LEFT_SIZE"].ToString();
                            dr["LEFT_QTY"] = dt_SizeParm.Rows[i]["LEFT_QTY"].ToString();
                            dr["RIGHT_SIZE"] = dt_SizeParm.Rows[i]["RIGHT_SIZE"].ToString();
                            dr["RIGHT_QTY"] = dt_SizeParm.Rows[i]["RIGHT_QTY"].ToString();
                            dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                            dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                            dtTemp2.Rows.Add(dr);
                        }
                    }
                    //更新尺寸参数【棉】
                    foreach (var foam in str1)
                    {
                        for (int i = 0; i < dt_SizeFoam.Rows.Count; i++)
                        {
                            DataRow dr = dtTemp4.NewRow();
                            dr["PART_NO"] = foam;
                            dr["MOUDLE_NAME"] = dt_SizeFoam.Rows[i]["MOUDLE_NAME"].ToString();
                            dr["FOAM_TYPE"] = dt_SizeFoam.Rows[i]["FOAM_TYPE"].ToString();
                            dr["REQUIRE_QTY"] = dt_SizeFoam.Rows[i]["REQUIRE_QTY"].ToString();
                            dr["FOAM_CUT_LEN"] = dt_SizeFoam.Rows[i]["FOAM_CUT_LEN"].ToString();
                            dr["FOAM_CUT_WIDTH"] = dt_SizeFoam.Rows[i]["FOAM_CUT_WIDTH"].ToString();
                            dr["FOAM_CUT_THICK"] = dt_SizeFoam.Rows[i]["FOAM_CUT_THICK"].ToString();
                            dr["FOAM_PROCESS"] = dt_SizeFoam.Rows[i]["FOAM_PROCESS"].ToString();
                            dr["RAW_MAT_SIZE"] = dt_SizeFoam.Rows[i]["RAW_MAT_SIZE"].ToString();
                            dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                            dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                            dtTemp4.Rows.Add(dr);
                        }
                    }
                    //更新部件特征表
                    foreach (var feat in str1)
                    {
                        for (int i = 0; i < dt_Feature.Rows.Count; i++)
                        {
                            DataRow dr = dtTemp3.NewRow();
                            dr["DWG_NO"] = "NIL";
                            dr["PART_NO"] = feat;
                            dr["PROCESS_CODE"] = dt_Feature.Rows[i]["PROCESS_CODE"].ToString();
                            dr["FEATURE_NAME"] = dt_Feature.Rows[i]["FEATURE_NAME"].ToString();
                            dr["FEAT_VALUE"] = dt_Feature.Rows[i]["FEAT_VALUE"].ToString();
                            dr["LABOR_VALUE"] = dt_Feature.Rows[i]["LABOR_VALUE"].ToString();
                            dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                            dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                            dtTemp3.Rows.Add(dr);
                        }
                    }
                    //更新周期表
                    foreach (var cycle in str1)
                    {
                        DataRow dr = dtTemp5.NewRow();
                        dr["PART_NO"] = cycle;
                        dr["CYCLE_TIME"] = dt_Cycle.Rows[0]["CYCLE_TIME"].ToString().Trim().Length > 0 ?
                             dt_Cycle.Rows[0]["CYCLE_TIME"].ToString().Trim() : "0";
                        dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                        dtTemp5.Rows.Add(dr);
                    }
                    if (BaseBuildSql.ExcuteInsert(dtTemp1, "IE_Part_Routing"))
                    {
                    }
                    if (BaseBuildSql.ExcuteInsert(dtTemp2, "IE_Part_SizeParmList_Fabric"))
                    {
                    }
                    if (BaseBuildSql.ExcuteInsert(dtTemp3, "IE_Part_FeatureList"))
                    {
                    }
                    if (BaseBuildSql.ExcuteInsert(dtTemp4, "IE_Part_SizeParmList_Foam"))
                    {
                    }
                    if (BaseBuildSql.ExcuteInsert(dtTemp5, "IE_Part_CycleTime"))
                    {
                    }
                    DevExpress.XtraEditors.XtraMessageBox.Show("批量维护成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    memoEdit1.Text = "";
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("参考号码【工艺路线】或【部件特征】或【周期】存在空值，不允许保存！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
            }
        }


        private void Frm_IE_Reference_Add_Resize(object sender, EventArgs e)
        {
            int Heights = panelControl3.Height;
            gc_Routing.Height = Convert.ToInt32(Math.Round(Convert.ToDouble(Convert.ToDouble(Heights) / 5), 1));
            gc_Fabic.Height = Convert.ToInt32(Math.Round(Convert.ToDouble(Convert.ToDouble(Heights) / 5), 1));
            gc_Feature.Height = Convert.ToInt32(Math.Round(Convert.ToDouble(Convert.ToDouble(Heights) / 5), 1)) * 2;
            gc_Foam.Height = Convert.ToInt32(Math.Round(Convert.ToDouble(Convert.ToDouble(Heights) / 5), 1));
        }

        private void txtReference_No_UcTextKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    //只针对部件号码
                    if (!string.IsNullOrEmpty(txtReference_No.Text.Trim()))
                    {
                        //参考号码存在任务清单表且上载标识为1
                        string sql = @"select PART_NO FROM [LamexMESManager].[dbo].[IE_Part_TaskList] where PART_NO='" + txtReference_No.Text + "' and UPLOAD_FLAG=1";
                        DataTable dt_TaskList = BaseBuildSql.GetDataTableBySql(sql);
                        if (dt_TaskList.Rows.Count > 0)
                        {
                            //取得周期表【IE_Part_CycleTime】
                            string sql_cycle = @"SELECT [PART_NO]
                                                  ,[CYCLE_TIME]
                                                  ,[UPDATE_TIME]
                                                  ,[UPDATE_USER]
                                              FROM [LamexMESManager].[dbo].[IE_Part_CycleTime] where PART_NO='" + txtReference_No.Text.Trim() + "'";
                            dt_Cycle = BaseBuildSql.GetDataTableBySql(sql_cycle);

                            //显示工艺路线
                            string sql_Routing = @"SELECT [DWG_NO]
                                                  ,[PART_NO]
                                                  ,R.[GROUP_CODE]
	                                              ,C.ROUTING_CODE
	                                              ,C.ROUTING_DESC
	                                              ,R.[UPDATE_USER]
                                                  ,[LAST_SAVE_TIME]
                                              FROM [LamexMESManager].[dbo].[IE_Part_Routing] R LEFT JOIN 
                                              [LamexMESManager].[dbo].IE_Routing_Combination C ON R.GROUP_CODE=C.GROUP_CODE WHERE R.PART_NO='" + txtReference_No.Text.Trim() + "'";
                            dt_Routing = BaseBuildSql.GetDataTableBySql(sql_Routing);
                            gridControl1.DataSource = dt_Routing;
                            //显示尺寸参数[布]
                            string sql_SizeParmList = @"SELECT [PART_NO]
                                                          ,[MOUDLE_NAME]
                                                          ,[FABRIC_TYPE]
                                                          ,[REQUIRE_QTY]
                                                          ,[FRONT_SIZE]
                                                          ,[FRONT_QTY]
                                                          ,[BACK_SIZE]
                                                          ,[BACK_QTY]
                                                          ,[LEFT_SIZE]
                                                          ,[LEFT_QTY]
                                                          ,[RIGHT_SIZE]
                                                          ,[RIGHT_QTY]
                                                          ,[UPDATE_TIME]
                                                          ,[UPDATE_USER]
                                                      FROM [LamexMESManager].[dbo].[IE_Part_SizeParmList_Fabric]  WHERE PART_NO='" + txtReference_No.Text.Trim() + "'";
                            dt_SizeParm = BaseBuildSql.GetDataTableBySql(sql_SizeParmList);
                            gridControl2.DataSource = dt_SizeParm;
                            //显示尺寸参数【棉】
                            string sql_sizeFoam = @"SELECT [PART_NO]
                                                      ,[MOUDLE_NAME]
                                                      ,[FOAM_TYPE]
                                                      ,[REQUIRE_QTY]
                                                      ,[FOAM_CUT_LEN]
                                                      ,[FOAM_CUT_WIDTH]
                                                      ,[FOAM_CUT_THICK]
                                                      ,[FOAM_PROCESS]
                                                      ,[RAW_MAT_SIZE]
                                                      ,[UPDATE_TIME]
                                                      ,[UPDATE_USER]
                                                  FROM [LamexMESManager].[dbo].[IE_Part_SizeParmList_Foam]  WHERE PART_NO='" + txtReference_No.Text.Trim() + "'";
                            dt_SizeFoam = BaseBuildSql.GetDataTableBySql(sql_sizeFoam);
                            gridControl4.DataSource = dt_SizeFoam;
                            //显示部件特征
                            string sql_Feature = @"SELECT [DWG_NO]
                                                  ,[PART_NO]
                                                  ,[PROCESS_CODE]
                                                  ,[FEATURE_NAME]
                                                  ,[FEAT_VALUE]
                                                  ,[LABOR_VALUE]
                                                  ,[UPDATE_TIME]
                                                  ,[UPDATE_USER]
                                              FROM [LamexMESManager].[dbo].[IE_Part_FeatureList]
                                              where PART_NO='" + txtReference_No.Text + "'";
                            dt_Feature = BaseBuildSql.GetDataTableBySql(sql_Feature);
                            gridControl3.DataSource = dt_Feature;
                            //Gridcontrol内容自使用
                            gridView1.OptionsView.ColumnAutoWidth = false;
                            gridView2.OptionsView.ColumnAutoWidth = false;
                            gridView3.OptionsView.ColumnAutoWidth = false;
                            gridView4.OptionsView.ColumnAutoWidth = false;
                            this.gridView1.BestFitColumns();
                            this.gridView2.BestFitColumns();
                            this.gridView3.BestFitColumns();
                            this.gridView4.BestFitColumns();

                        }
                        else
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("参考号码不存在任务清单表或没有上载SAP！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtReference_No.Text = "";
                            return;
                        }
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("参考号码不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
                }
            }
        }

    }
}
